﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Observer
{
    /// <summary>
    /// ListColorFrame.xaml 的交互逻辑
    /// </summary>
    public partial class ListColorFrame : Window, IUpdate, IIterator
    {
        public void UpdateListColorFrame( int i )
        {
            addItem( i );
        }

        public void OnUpdate( int i )
        {
            if ( fuck )
            {
                fuck = false;
                return;
            }
            UpdateListColorFrame( i );
        }

        OnUpdateDelegate Update;

        public ListColorFrame()
        {
            InitializeComponent();
        }

        public void SetUpdate( OnUpdateDelegate update )
        {
            Update = update;
        }

        void addItem( int add )
        {
            string additem = "";
            switch ( add )
            {
                case 1:
                    additem = "Red";
                    break;
                case 2:
                    additem = "Green";
                    break;
                case 3:
                    additem = "Blue";
                    break;
            }

            ListColorBox.SelectedIndex = ListColorBox.Items.Add( additem );
            object d = ListColorBox.SelectedItem;
        }

        int GetColorIndex( string color )
        {
            switch ( color )
            {
                case "Red":
                    return 1;
                case "Green":
                    return 2;
                case "Blue":
                    return 3;
            }
            return 0;
        }

        #region IIterator 成员

        bool fuck = false;

        public void Next()
        {
            ListColorBox.SelectedIndex =
                ListColorBox.SelectedIndex == ListColorBox.Items.Count - 1 ? ListColorBox.Items.Count - 1 : ListColorBox.SelectedIndex + 1;
            string s =  ListColorBox.SelectedItem as string;
            fuck = true;
            Update( GetColorIndex( s ) );
        }

        public void Prev()
        {
            ListColorBox.SelectedIndex = ListColorBox.SelectedIndex > 0 ? ListColorBox.SelectedIndex - 1 : 0;
            //ListColorBox.SelectedItem = ListColorBox.Items.CurrentItem
            string s =  ListColorBox.SelectedItem as string;
            fuck = true;

            Update( GetColorIndex( s ) );
        }

        public object CurrentItem()
        {
            return ListColorBox.Items.CurrentItem;
        }

        #endregion

        private void button1_Click( object sender, RoutedEventArgs e )
        {
            Prev();
        }

        private void button2_Click( object sender, RoutedEventArgs e )
        {
            Next();
        }

        private void Window_Loaded( object sender, RoutedEventArgs e )
        {

        }
    }
}
